﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Net5ApiAndEfMySql.DtoModel;
using Net5ApiAndEfMySql.IRepository.SystemAdmin;
using Net5ApiAndEfMySql.PoModel.SystemAdmin;

namespace Net5ApiAndEfMySql.Repository.SystemAdmin
{
    public class RoleDbRepository : BaseRepository, IRoleDbRepository
    {
        //public RoleDbPo GetRoleByOprole(string userName)
        //{
        //    //获取用户角色关系表的Queryable
        //    var aurps = SoaSystemCenterDbContext.OperatorPo.AsQueryable();
        //    //获取角色表的Queryable
        //    var rdps = SoaSystemCenterDbContext.RoleDbPo.AsQueryable();
        //    //用linq实现查询对应用户名的角色的 Queryable
        //    var query = from aur in aurps
        //        join
        //            rdp in rdps on aur.op_role equals rdp.role_id
        //        where aur.op_account == userName
        //        select rdp;
        //    //执行Queryable 查询数据库 返回数据
        //    RoleDbPo roleList = query.ToList().FirstOrDefault();
        //    return roleList;
        //}

        public string GetRoleByOprole(string userName)
        {
            //获取用户角色关系表的Queryable
            var aurps = SoaSystemCenterDbContext.OperatorPo.AsQueryable();
            //获取角色表的Queryable
            var rdps = SoaSystemCenterDbContext.RoleDbPo.AsQueryable();
            //用linq实现查询对应用户名的角色的 Queryable
            var query = from aur in aurps
                join
                    rdp in rdps on aur.op_role equals rdp.role_id
                where aur.op_account == userName
                select rdp;
            //执行Queryable 查询数据库 返回数据
            string roleList = query.ToList().FirstOrDefault().role_name;
            return roleList;
        }
        /// <summary>
        /// 用户分页查询
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public PagerResult<RoleDbPo> GetPageList(SearchModel m)
        {
            IQueryable<RoleDbPo> iq = SoaSystemCenterDbContext.RoleDbPo.AsQueryable();
            if (m.SearchItem != null && m.SearchItem.Count > 0)
            {
                var tempDi = m.SearchItem;
                string userName = "role_name";
                if (tempDi.Keys.Contains(userName) && !string.IsNullOrEmpty(tempDi[userName]))
                {
                    iq = iq.Where(c => c.role_name.Contains(tempDi[userName]));
                }
            }
            return Pager(iq, m);
        }
        /// <summary>
        /// 角色表的添加
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool AddRole(RoleDbPo role)
        {
            //添加操作
            SoaSystemCenterDbContext.Add(role);
            //保存操作
            SoaSystemCenterDbContext.SaveChanges();
            //返回值
            return true;
        }
        /// <summary>
        /// 反填角色表
        /// </summary>
        /// <param name="bid"></param>
        /// <returns></returns>
        public RoleDbPo GetRoleById(int role_id)
        {
            //根据角色表Id查找对应的数据进行反填
            return SoaSystemCenterDbContext.RoleDbPo.FirstOrDefault(c => c.role_id == role_id);
        }
        /// <summary>
        /// 修改角色表
        /// </summary>
        /// <param name="but"></param>
        /// <returns></returns>
        public bool UpdateRoleDbPo(RoleDbPo role)
        {
            //如果角色表等于null则返回
            if (role == null) return false;
            //创建一个事务
            using (var tran = SoaSystemCenterDbContext.Database.BeginTransaction())
            {
                try
                {
                    //根据上下文获取角色表的状态，获取角色表的所有字段进行修改
                    SoaSystemCenterDbContext.Entry(role).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    //保存操作
                    SoaSystemCenterDbContext.SaveChanges();
                    //提交事务
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    //回滚事务
                    tran.Rollback();
                }
            }
            //获取返回值
            return true;
        }

        /// <summary>
        /// 删除角色表
        /// </summary>
        /// <param name="role_id"></param>
        /// <returns></returns>
        public bool DeleteRoleDbPo(int role_id)
        {
            //定义list变量用来获取要删除数据的Id
            var list = SoaSystemCenterDbContext.RoleDbPo.Find(role_id);
            //删除操作
            SoaSystemCenterDbContext.RoleDbPo.Remove(list);
            //保存操作
            SoaSystemCenterDbContext.SaveChanges();
            //获取返回值
            return true;
        }

        public List<RoleDbPo> GetRoleListByUserName(int os_id)
        {
            throw new NotImplementedException();
        }
        public List<RoleDbPo> GetRoleListBypositioninfo(int po_id)
        {
            throw new NotImplementedException();
        }
    }
}
